#include<stdio.h>
int binary_search(int a[], int k, int s)
{
	int left = 0;
	int right = s - 1;

	while (left <= right)
	{
		int mid = (left + right) / 2;
		if (a[mid] > k)
		{
			right = mid - 1;
		}
		else if (a[mid] < k)
		{
			left = mid + 1;
		}
		else {
			return mid;
		}
	}
	return -1;
}//二分查找的时间复杂度为logN

int main() {
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int key = 7;
	int se = sizeof(arr)/sizeof(arr[0]);
	//printf("%d", sizeof(arr)/sizeof(arr[0]));打印字符串长度
	int ret = binary_search(arr, key, se);
	if (-1 == ret)
	{
		printf("找不到\n");
	}
	else {
		printf("找到了，下标是：%d\n", ret);
	}
	return 0;
}